import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  strict:true,
 state(){
   return {
    isCon:'all',
    list:JSON.parse(localStorage.getItem('list'))||[
      { id: 100, name: "吃饭", isDone: true },
      { id: 201, name: "睡觉", isDone: false },
      { id: 103, name: "打豆豆", isDone: true },
    ],
  
   }
 },
 mutations:{
   newList(state,obj){
      state.list.push(obj)
   },
   del(state,id){
     const index = state.list.findIndex(item=>item.id===id)
    state.list.splice(index,1)
   },
   switchover(state,value){
    state.isCon = value
   },
   allDel(state){
    state.list = state.list.filter(item=>!item.isDone)
   },
   checked(state,value){
    state.list.forEach(item => {
      item.isDone = value
    });
   },
   changeAll(state,id){
     const index = state.list.findIndex(item=>item.id === id)
    state.list[index].isDone = !state.list[index].isDone
   }
 },
 getters:{
  
  cut(state){
    if(state.isCon === 'yes'){
    return  state.list.filter(item=>item.isDone)
    }else if(state.isCon === 'on'){
      return state.list.filter(item=>!item.isDone)
    }else{
      return  state.list
    }
  },

 },
//数据缓存到本地  
})
export default store